/**
* This looks at static needs parameter in components and waits for the promise to be fullfilled
* It is used to make sure server side rendered pages wait for APIs to resolve before returning res.end()
* As seen in: https://github.com/caljrimmer/isomorphic-redux-app
*/

export function fetchComponentDataBeforeRender(dispatch, components, params) {
  const needs = components
      .filter(x=>x.fetchData)
      .reduce((prev, current) => {
    return (current.need || [])
        //.fetchData(params)
        .concat((current.WrappedComponent ? current.WrappedComponent.need : []) || [])
        .concat(prev);
    }, []);
    const promises = needs.map(need => dispatch(need(params)));
    return Promise.all(promises);
}
export function fetchAllData(dispatch, components, params) {
  const needs = components
      .filter(x=>x.fetchData)
      .reduce((prev,current)=>{
        return current.fetchData(params).concat(prev)
      },[])
      .map(x=>{
        return dispatch(x)
      })
  return Promise.all(needs)
}